//
//  SegmentTableViewController.m
//  QingSongYiKe
//
//  Created by lanou3g on 16/3/22.
//  Copyright © 2016年 wanshuai. All rights reserved.
//

#import "SegmentTableViewController.h"
#import "PassValueForPhotoTitle.h"
#import "PhotoTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CDHttpHelper.h"
#import "PhotoModel.h"
@interface SegmentTableViewController ()
@property(nonatomic,strong)NSMutableArray *photoDataArray;

@property(nonatomic,strong)NSMutableArray *photoDataArray1;
@end

@implementation SegmentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:(UIBarButtonItemStyleDone) target:self action:nil];
    
    
    [self handleData];
    
    [self.tableView registerClass:[PhotoTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)handleData
{
    self.photoDataArray = [NSMutableArray array];
    self.photoDataArray1 = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://s.budejie.com/topic/list/jingxuan/10/budejie-android-6.2.0/1458603842-20.json"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        for (NSDictionary *dic in dict[@"list"]) {
            
           
            
            PhotoModel *photoModel = [PhotoModel new];
            photoModel.title = dic[@"text"];
            [self.photoDataArray addObject:photoModel];
            
            NSArray *arr = dic[@"gif"][@"images"];
            if (arr.count > 1) {
                [self.photoDataArray1 addObject:arr[0]];
                
            }
            
            
        }
        
    }];
    dispatch_async(dispatch_get_main_queue(), ^{
       
        [self.tableView reloadData];
    });
    
    [dataTask resume];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.photoDataArray1.count;

   
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (cell == nil) {
        
        cell = [[PhotoTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
    }
    PassValueForPhotoTitle *passValue = [PassValueForPhotoTitle shareValue];
    if ([passValue.title isEqualToString:@"GIF"]) {
      
        PhotoModel *photo = self.photoDataArray[indexPath.row];
        cell.titleLable.text = photo.title;
        [cell.photoImageView sd_setImageWithURL:self.photoDataArray1[indexPath.row]];
    }else if ([passValue.title isEqualToString:@"美女"]){
        
        
    }else{
        
        
        
    }
    
    
    
   
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 350;
    
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
